package main

import (
	"go_web/test_demo/demo7_action/model"
	"html/template"
	"net/http"
)

func testIf(w http.ResponseWriter, r *http.Request) {
	// 解析模板
	t := template.Must(template.ParseFiles("test_demo/demo7_action/if.html"))
	age := 17
	// 执行
	t.Execute(w, age < 18)
}
func testRange(w http.ResponseWriter, r *http.Request) {
	// 解析模板
	t := template.Must(template.ParseFiles("test_demo/demo7_action/range.html"))
	emps := make([]*model.Employee, 0, 3)
	emp1 := &model.Employee{
		ID:       1,
		LastName: "李小璐",
		Email:    "lxl@jnl.com",
	}
	emps = append(emps, emp1)
	emp2 := &model.Employee{
		ID:       2,
		LastName: "白百合",
		Email:    "bbh@cyq.com",
	}
	emps = append(emps, emp2)
	emp3 := &model.Employee{
		ID:       3,
		LastName: "马蓉",
		Email:    "mr@wbq.com",
	}
	emps = append(emps, emp3)

	// 执行
	t.Execute(w, emps)
}
func testWith(w http.ResponseWriter, r *http.Request) {
	// 解析模板
	t := template.Must(template.ParseFiles("test_demo/demo7_action/with.html"))
	// 执行
	t.Execute(w, "狸猫")
}
func testTemplate(w http.ResponseWriter, r *http.Request) {
	// 解析模板
	t := template.Must(template.ParseFiles("test_demo/demo7_action/template1.html", "test_demo/demo7_action/template2.html"))
	// 执行
	t.Execute(w, "能在两个文件中显示")
}
func testDefine(w http.ResponseWriter, r *http.Request) {
	// 解析模板
	t := template.Must(template.ParseFiles("test_demo/demo7_action/define.html"))
	// 执行
	t.ExecuteTemplate(w, "model", "")
}
func testDefine2(w http.ResponseWriter, r *http.Request) {
	var t *template.Template
	// 解析模板
	if true {
		t = template.Must(template.ParseFiles("test_demo/demo7_action/define2.html", "test_demo/demo7_action/content2.html"))
	} else {
		t = template.Must(template.ParseFiles("test_demo/demo7_action/define2.html", "test_demo/demo7_action/content1.html"))
	}
	// 执行
	t.ExecuteTemplate(w, "model", "")
}
func main() {
	http.HandleFunc("/testIf", testIf)
	http.HandleFunc("/testRang e", testRange)
	http.HandleFunc("/testWith", testWith)
	http.HandleFunc("/testTemplate", testTemplate)
	http.HandleFunc("/testDefine", testDefine)
	http.HandleFunc("/testDefine2", testDefine2)
	http.ListenAndServe(":8080", nil)
}
